package com.liujit.bms.pc.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.pc.domain.model.BlogArticle;
import com.liujit.bms.pc.domain.model.BlogCategory;
import com.liujit.bms.pc.domain.result.BlogCategoryResult;
import com.liujit.bms.pc.mapper.BlogCategoryMapper;
import com.liujit.bms.pc.service.BlogArticleService;
import com.liujit.bms.pc.service.BlogCategoryService;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 博客分类Service实现类
 * @Author liujun
 * @Date 2021-07-03 16:03:30
 */
@Service
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory>
        implements BlogCategoryService {

    private BlogArticleService blogArticleService;

    @Autowired
    public void setBlogArticleService(BlogArticleService blogArticleService) {
        this.blogArticleService = blogArticleService;
    }

    @Override
    public List<BlogCategoryResult> getList() {
        List<BlogCategoryResult> list = new ArrayList<>();
        // 查询文章分类数量
        QueryWrapper<BlogArticle> articleWrapper = new QueryWrapper<>();
        articleWrapper.select("category_id, count(id) as num");
        articleWrapper.groupBy("category_id");
        List<Map<String, Object>> articleList = blogArticleService.listMaps(articleWrapper);
        Map<Long, Integer> articleMap = articleList.stream().collect(Collectors
                .toMap(item -> Convert.toLong(item.get("category_id")), item -> Convert.toInt(item.get("num"))));
        LambdaQueryWrapper<BlogCategory> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogCategory::getEnabled, GlobalConstant.ENABLED_YES);
        wrapper.orderByAsc(BlogCategory::getSeq);
        List<BlogCategory> blogCategoryList = super.list(wrapper);
        blogCategoryList.forEach(item -> {
            BlogCategoryResult result = DozerConvertor.convertor(item, BlogCategoryResult.class);
            Integer articleCount = articleMap.get(result.getId());
            if (articleCount == null) {
                result.setArticleCount(0);
            } else {
                result.setArticleCount(articleCount);
            }
            list.add(result);
        });
        return list;
    }

    @Override
    public BlogCategoryResult get(Long id) {
        BlogCategory blogCategory = super.getById(id);
        if (blogCategory == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(blogCategory, BlogCategoryResult.class);
    }

    @Override
    public String getNameById(Long categoryId) {
        LambdaQueryWrapper<BlogCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BlogCategory::getName);
        wrapper.eq(BlogCategory::getId, categoryId);
        return super.getObj(wrapper, item -> Convert.toStr(item));
    }
}
